//
//  CZRefreshView.swift
//  刷新控件
//
//  Created by 名品导购网MPLife.com on 2017/12/7.
//  Copyright © 2017年 sweet. All rights reserved.
//

import UIKit
//刷新视图 负责刷新相关的UI显示和动画
class CZRefreshView: UIView {
    //刷新状态
    private lazy var backimgaeView:UIImageView = UIImageView()
    private lazy var indicator:UIActivityIndicatorView = UIActivityIndicatorView()
    private lazy var tipIcon:UIImageView = UIImageView()
    private lazy var tipLabel:UILabel = UILabel()
  /*
     IOS系统中UIViewz封装旋转动画
     默认顺时针旋转
     就近原则
     想要实现同方向旋转 需要调整一个非常小的数字
     如果想实现360旋转，需要核心动画CABaseAnimation
  */
    var refreshState:CZRefreshState = .Normal {
        didSet{
            switch refreshState {
            case .Normal:
                tipIcon.isHidden = false
                indicator.stopAnimating()
                tipLabel.text = "继续使劲拉..."
                UIView .animate(withDuration: 0.25, animations: {
                    //恢复到原始状态
                    self.tipIcon.transform = CGAffineTransform.identity
                })
            case  .Pulling:
                tipLabel.text = "放手就刷新..."
                UIView .animate(withDuration: 0.25, animations: {
                    //旋转一百八十度
                    self.tipIcon.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi + 0.001))//减一个很小的数值，使其不要顺时针转
                })
            case  . WillRefresh:
                //隐藏提示图标 显示菊花
                tipLabel.text = "正在刷新中..."
                tipIcon.isHidden = true
                indicator .startAnimating()
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
//        setUPUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
}

extension CZRefreshView {
    func setUPUI() {
//        let backView = UIView(frame: CGRect(x: (UIScreen.cz_screenWidth()-155)/2, y: 0, width: 120, height: 60))
        let backView = UIView(frame: CGRect(x: (UIScreen.main.bounds.width-155)/2, y: 0, width: 120, height: 60))
        self.addSubview(backView)
        
//        backimgaeView.frame = CGRect(x: 0, y: -438+60, width: UIScreen.cz_screenWidth(), height: 438)
        backimgaeView.frame = CGRect(x: 0, y: -438+60, width: UIScreen.main.bounds.width, height: 438)
        backimgaeView.image = UIImage(named: "refreshbg")
        self.addSubview(backimgaeView)
        
        indicator.frame = CGRect(x: 0, y: 15, width: 20, height: 30)
        indicator.activityIndicatorViewStyle = .gray
        indicator.hidesWhenStopped = true
        backView.addSubview(indicator)
        
        tipIcon.frame = CGRect(x: 0, y: 20, width: 20, height: 20)
        tipIcon.image = UIImage(named: "tableview_pull_refresh")
        backView.addSubview(tipIcon)
        
        tipLabel.frame = CGRect(x: 30, y: 20, width: 110, height: 20)
        tipLabel.font = UIFont.systemFont(ofSize: 15)
        tipLabel.text = "下拉开始刷新..."
        backView.addSubview(tipLabel)
    }
}
